home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue61 / Stream / ScreenSaver.dpr < prev    next >
Encoding:
Text File  |  2000-04-23  |  3.5 KB  |  134 lines

  1. program ScreenSaver;
  2.  
  3. uses
  4.   Forms,
  5.   SysUtils,
  6.   Windows,
  7.   Graphics,
  8.   Classes,
  9.   Dialogs,
  10.   Controls,
  11.   uSetup in 'uSetup.pas' {frmSetup},
  12.   uScreenSaver in 'uScreenSaver.pas' {frmScrn},
  13.   uGlobals in 'uGlobals.pas';
  14.  
  15. {$E DAT}
  16.  
  17. {$R *.RES}
  18.  
  19. var
  20.   MySem       : THandle;
  21.   Arg1,
  22.   Arg2        : String;
  23.   DemoWnd     : HWnd;
  24.   MyRect      : TRect;
  25.   ScrWidth,
  26.   ScrHeight   : Integer;
  27.   SysDir      : String;
  28.   NewLen      : Integer;
  29.   MyMod       : THandle;
  30.   PwdFunc     : function (a : PChar; ParentHandle : THandle; b, c : Integer) :
  31.                     Integer; stdcall;
  32.   fs : TFileStream;
  33.   iSize, j : integer;
  34.   Buf : array[0..19] of Char;
  35.   ssi : TSSImage;
  36.   TheCanvas : TCanvas;
  37.   
  38. begin
  39.   try
  40.      Arg1 := UpperCase(ParamStr(1));
  41.      Arg2 := UpperCase(ParamStr(2));
  42.  
  43.      if (Copy(Arg1,1,2) = '/A') or (Copy(Arg1,1,2) = '-A') or
  44.         (Copy(Arg1,1,1) = 'A') then
  45.        SSMode := ssSetPwd;
  46.  
  47.      if (Copy(Arg1,1,2) = '/P') or (Copy(Arg1,1,2) = '-P') or
  48.         (Copy(Arg1,1,1) = 'P') then
  49.        SSMode := ssPreview;
  50.  
  51.      if (Copy(Arg1,1,2) = '/C') or (Copy(Arg1,1,2) = '-C') or
  52.         (Copy(Arg1,1,1) = 'C') or (Arg1 = '') then
  53.        SSMode := ssConfig;
  54.  
  55.      if SSMode = ssSetPwd then begin
  56.        SetLength(SysDir,MAX_PATH);
  57.        NewLen := GetSystemDirectory(PChar(SysDir),MAX_PATH);
  58.        SetLength(SysDir,NewLen);
  59.        if (Length(SysDir) > 0) and (SysDir[Length(SysDir)] <> '\') then
  60.          SysDir := SysDir+'\';
  61.        MyMod := LoadLibrary(PChar(SysDir+'MPR.DLL'));
  62.        if MyMod <> 0 then begin
  63.          PwdFunc := GetProcAddress(MyMod,'PwdChangePasswordA');
  64.          if Assigned(PwdFunc) then
  65.            PwdFunc('SCRSAVE',StrToInt(Arg2),0,0);
  66.          FreeLibrary(MyMod);
  67.        end;
  68.        Halt;
  69.      end;
  70.  
  71.      MySem := CreateSemaphore(nil,0,1,PChar(ExtractFileName(Application.ExeName)+'Semaphore'));
  72.      if ((MySem <> 0) and (GetLastError = ERROR_ALREADY_EXISTS)) then begin
  73.        CloseHandle(MySem);
  74.        Halt;
  75.      end;
  76.  
  77.      Application.Initialize;
  78.      Application.ProcessMessages;
  79.  
  80.      if SSMode = ssPreview then
  81.      begin
  82.        DemoWnd := StrToInt(Arg2);
  83.        while not IsWindowVisible(DemoWnd) do
  84.          Application.ProcessMessages;
  85.        GetWindowRect(DemoWnd,MyRect);
  86.  
  87.        ScrWidth := MyRect.Right-MyRect.Left+1;
  88.        ScrHeight := MyRect.Bottom-MyRect.Top+1;
  89.  
  90.        TheCanvas := TCanvas.Create;
  91.        fs := TFileStream.Create( Application.ExeName, fmOpenRead or fmShareDenyWrite );
  92.        try
  93.          fs.Position := fs.Size-40;
  94.          j := fs.Read(Buf,20);
  95.          if j <> 20 then exit;
  96.          iSize := StrToIntDef(Trim(buf),0);
  97.  
  98.          fs.Position := fs.Size-iSize-40;
  99.  
  100.          TheCanvas.Handle := GetDC(DemoWnd);
  101.          ssi := TSSImage(fs.ReadComponent(nil));
  102.          try
  103.             TheCanvas.StretchDraw(Rect(0,0,ScrWidth,ScrHeight),ssi.Picture.Graphic);
  104.          finally
  105.             ssi.Free;
  106.          end;
  107.          while IsWindowVisible(DemoWnd) do
  108.          begin
  109.            Application.ProcessMessages;
  110.            if not IsWindowVisible(DemoWnd) then break;
  111.          end;
  112.        finally
  113.          fs.free;
  114.          TheCanvas.Free;
  115.        end;
  116.  
  117.        CloseHandle(MySem);
  118.        Halt;
  119.      end;
  120.  
  121.      if SSMode = ssConfig then begin
  122.        Application.CreateForm(TfrmSetup, frmSetup);
  123.      end else
  124.        Application.CreateForm(TfrmScrn,frmScrn);
  125.  
  126.      Application.Run;
  127.      Application.ProcessMessages;
  128.  
  129.      CloseHandle(MySem);
  130.   except
  131.   end;
  132. end.
  133.  
  134.